package weapons;

import gui.MainFrame;
import java.awt.Image;
import java.awt.Rectangle;
import playerData.PlayerMove;

public class Weapon extends Thread {

    private boolean collide, pause, explosion;
    private int x, y;
    private final boolean direction; //true=left, false=right;
    private PlayerMove pm1, pm2;
    private final Image weaponImgR, weaponImgL, weaponImg;
    private final int imageWidth, imageHeight;
    private final Rectangle rectWeapon;

    public Weapon(boolean direction, PlayerMove shooter, PlayerMove pm1, PlayerMove pm2, Image weaponImgL, Image weaponImgR) {
        collide = false;
        pause = false;
        this.weaponImgL = weaponImgL;
        this.weaponImgR = weaponImgR;
        this.direction = direction;
        if (direction) {
            imageWidth = weaponImgL.getWidth(null) / 4;
            imageHeight = weaponImgL.getHeight(null) / 4;
            weaponImg = weaponImgL;
            x = shooter.getX() - imageWidth - 3;
            y = shooter.getY() + (shooter.getImageHeight() / 4);
        } else {
            imageWidth = weaponImgR.getWidth(null) / 4;
            imageHeight = weaponImgR.getHeight(null) / 4;
            weaponImg = weaponImgR;
            x = shooter.getX() + shooter.getImageWidth() + 3;
            y = shooter.getY() + (shooter.getImageHeight() / 4);
        }
        rectWeapon = new Rectangle(x, y, imageWidth, imageHeight);
        this.pm1 = pm1;
        this.pm2 = pm2;
        explosion = false;
        this.start();
    }

    @Override
    public void run() {
        while (!collide) {
            if (pause) {
                continue;
            }
            try {
                moveWeapon();
                Weapon.sleep(15);
            } catch (InterruptedException ex) {
            }
        }
    }

    private void moveWeapon() {
        if (direction) {
            x -= 5;
        } else {
            x += 5;
        }
        PlayerMove pmC = checkCollide();
        if (pmC != null) {
            pmC.receiveDamage(20);
        }
    }

    private PlayerMove checkCollide() {
        //TODO cut pictures smaller

        //TODO after finishing the performance test
        performanceTester.Performance.setTime1();
        
        //Überprüft ob die Waffe den Spieler 1 berührt
        if (pm1.collide(x, y, imageWidth, imageHeight)) {
            collide = true;
            return pm1;
        }

        //Überprüft ob die Waffe den Spieler 2 berührt
        if (pm2.collide(x, y, imageWidth, imageHeight)) {
            collide = true;
            return pm2;
        }

        //Überprüft wann die Waffe die Spielfläche verlässt
        if ((x + imageWidth < 1) || (x > MainFrame.pnSpielflaeche.getWidth())) {
            collide = true;
            return null;
        }

        //TODO after finishing the performance test
        performanceTester.Performance.setTime2();
        return null;
    }

    public Image getWeaponImg() {
        return weaponImg;
    }

    public int getImageWidth() {
        return imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean getCollide() {
        return collide;
    }

    public boolean getExplosion() {
        return explosion;
    }

    public void pauseThread() {
        pause = true;
    }

    public void resumeThread() {
        pause = false;
    }

    public void stopThread() {
        collide = true;
    }
}
